package woaini.fenger.core.bot.session;

import cn.hutool.extra.spring.SpringUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import woaini.fenger.core.bind.domain.BotBind;
import woaini.fenger.core.bind.service.BotUserConfigService;
import woaini.fenger.core.bot.base.Bot;
import woaini.fenger.core.bot.enums.PostTypeEnums;
import woaini.fenger.core.bot.event.BaseEvent;
import woaini.fenger.core.bot.event.Messages;
import woaini.fenger.core.bot.event.message.GroupMessageEvent;
import woaini.fenger.core.bot.event.message.PrivateMessageEvent;
import woaini.fenger.core.bot.id.ID;

/**
 * 会话
 *
 * @see woaini.fenger.core.bot.session.Session
 * @author yefeng {@code @Date} 2023-05-16 16:50:39
 */
@Getter
@Setter
@Slf4j
public class Session extends ApplicationEvent {

  private Bot bot;

  private BaseEvent event;

  private BotBind bind;

  public Session(Object source) {
    super(source);
  }

  /**
   * @MethodName getBotUserConfig
   *
   * @param configKey 配置键
   * @param rClass R级
   * @author yefeng {@date 2024-01-29 17:28:15}
   * @since 1.0
   * @return {@link R } 获取机器人用户配置
   */
  public <R> R getCurrentUserConfig(String configKey, Class<R> rClass) {
    BotUserConfigService bean = SpringUtil.getBean(BotUserConfigService.class);
    return bean.getBotUserConfig(this.bind.getBotUser().getId().toString(), configKey, rClass);
  }

  public <R> R getUserConfig(String id,String configKey, Class<R> rClass) {
    BotUserConfigService bean = SpringUtil.getBean(BotUserConfigService.class);
    return bean.getBotUserConfig(id, configKey, rClass);
  }

  /**
   * @MethodName getActualText
   *
   * @author yefeng {@date 2024-01-24 17:54:40}
   * @since 1.0
   * @return {@link String } 获取纯文本消息
   */
  public String getActualText() {
    String actualText = null;
    boolean isMessageEvent = event.getPostType().equals(PostTypeEnums.message);
    if (!isMessageEvent) {
      return actualText;
    }
    // 获取纯文本信息
    try {
      if (event instanceof PrivateMessageEvent) {
        // 提取文本信息
        actualText = ((PrivateMessageEvent) event).getMessage().actualText();
      } else if (event instanceof GroupMessageEvent) {
        actualText = ((GroupMessageEvent) event).getMessage().actualText();
      }
    } catch (Exception e) {
      log.error("消息解析异常", e);
    }
    return actualText;
  }

  public void replyMessage(Messages messages) {

    ID userId = this.event.getSender().getUserId();
    if (event instanceof PrivateMessageEvent) {
      this.bot.getBotAdapter().sendMessage("private", userId, null, messages, false);
    } else if (event instanceof GroupMessageEvent) {
      ID groupId = ((GroupMessageEvent) event).getGroupId();
      if (!messages.isAt(userId)) {
        messages.at(userId);
      }
      this.bot
          .getBotAdapter()
          .sendMessage("group", event.getSender().getUserId(), groupId, messages, false);
    }
  }

  public void replyMessage(String text) {
    replyMessage(new Messages().text(text));
  }
}
